package exp1.test1;

import junit.framework.TestCase;
import org.junit.Test;

import java.util.LinkedList;

/**
 * Created by 春旺 on 2017/9/24.
 */
public class LinkedListTest extends TestCase {
    LinkedList link = new LinkedList();
    @Test
    public void testAdd(){
       assertEquals(true, link.add(0));
       link.add(1);
        link.add(1);
       link.add(3);
       link.add(2,2);
       assertEquals("add",2,link.get(2));
       assertEquals("add",1,link.get(3));
      LinkedList a = new LinkedList();
      a.add(4);
      a.add(5);
      link.addAll(a);
      assertEquals("addAll",4,link.get(5));
      link.addAll(1,a);
      assertEquals("addAll -有参数",4,link.get(1));
      a.addFirst(1);
      a.addLast(6);
      assertEquals("addFirst",1,a.get(0));
      assertEquals("addLast",6,a.get(3));

    }
    @Test
    public void testClear(){
        link.add(1);
        link.add(1);
        link.add(3);
        assertEquals(3,link.size());
        link.clear();
        assertEquals(0,link.size());

    }
    @Test
    public void testContains(){
        assertEquals(false,link.contains("20162324"));
        link.add("1");
        link.add("2");
        link.add("20162324");
        assertEquals(false,link.contains(3));
        assertEquals(true,link.contains("20162324"));
    }
    @Test
    public void testGet(){

        for (int i = 0;i < 100;i++ ){
            link.add(i);
        }
        assertEquals("element",0,link.element());
        assertEquals("get",0,link.get(0));
        assertEquals("get",99,link.get(99));
        assertEquals("getFirst",0,link.getFirst());
        assertEquals("getLast",99,link.getLast());
        link.add(1);
        link.add(100);
        assertEquals("indexOf",1,link.indexOf(1));
        assertEquals("indexOf",101,link.indexOf(100));
        assertEquals("lastIndexOf",100,link.lastIndexOf(1));
    }
    @Test
    public void testOffer(){
        for (int i = 1;i < 21;i++ ){
            link.add(i);
        }
        link.offer(21);
        assertEquals("offer",21,link.size());
        assertEquals("offer",21,link.get(20));
        link.offerFirst(0);
        assertEquals("offerFirst",22,link.size());
        assertEquals("offerFirst",0,link.get(0));
        link.offerLast(22);
        assertEquals("offerLast",22,link.getLast());
    }

    @Test
    public void testPeek(){
        assertEquals("peek",null,link.peek());
        assertEquals("peekFirst",null,link.peekFirst());
        assertEquals("peekLast",null,link.peekLast());
        for (int i = 1;i < 21;i++ ){
            link.add(i);
        }
        assertEquals("peek",1,link.peek());
        assertEquals("peekFirst",1,link.peekFirst());
        assertEquals("peekLast",20,link.peekLast());
    }
    @Test
    public void testPoll(){
        assertEquals("poll",null,link.poll());
        assertEquals("poolFirst",null,link.pollFirst());
        assertEquals("pollLast",null,link.pollLast());
        for (int i = 1;i < 21;i++ ){
            link.add(i);
        }
        assertEquals(20,link.size());
        assertEquals("poll",1,link.poll());
        assertEquals(19,link.size());
        assertEquals("poolFirst",2,link.pollFirst());
        assertEquals(18,link.size());
        assertEquals("pollLast",20,link.pollLast());
        assertEquals(17,link.size());
    }
    @Test
    public void testP(){
        assertEquals(0,link.size());
        link.push("2");
        assertEquals("push",1,link.size());
        assertEquals("pop","2",link.pop());
        assertEquals(0,link.size());
    }
    @Test
    public void testRomve(){
        for (int j = 1;j < 11;j++)
        for (int i = 1;i < 6;i++ ){
            link.add(i);
        }
        assertEquals("remove",1,link.remove());
        assertEquals("remove--有参数",3,link.remove(1));
        assertEquals("removeFirst",2,link.removeFirst());
        assertEquals("removeLast",5,link.removeLast());
        assertEquals("removeLastOccurrence",true,link.removeLastOccurrence(4));
        assertEquals("removeLastOccurrence",false,link.removeLastOccurrence(6));
        assertEquals("removeLastOccurrence",3,link.removeLast());
        assertEquals("removeFirstOccurrence",true,link.removeFirstOccurrence(4));
        assertEquals("removeFirstOccurrence",false,link.removeFirstOccurrence(6));
        assertEquals("removeFirstOccurrence",5,link.removeFirst());
    }

}
